package com.jade.framework.web.spring;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

/**
 * 全局数据绑定扩展，用于处理日期和字符串。
 *
 * @author lyyang
 * @version 1.0 13-4-22 下午10:38
 */
public class BindingInitializerEx implements WebBindingInitializer
{
    protected String datePattern = "yyyy-MM-dd";

    public void setDatePattern(String datePattern)
    {
        this.datePattern = datePattern;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request)
    {
        //可以設定任意的日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    }
}